<style lang="less" scoped>
.el-dialog--center .el-dialog__body {
  padding: 17px 30px 41px 40px;
}

/deep/.el-form-item__label {
  padding-right: 0;
}

.el-radio {
  margin-right: 0;
}

/deep/.el-dialog__header {
  // height: 50px;
  line-height: 2em;
}

/deep/ .white-head .el-dialog__header {

  background-color: white;
  color: #2b354a;
}

.warningDiv {
  color: rgba(239, 137, 0, 1);
  ::v-deep .iconfont {
    line-height: 20px;
  }


  // font-size: 50px;
}
</style>
<style lang="less">
.el-message-box {
  width: 500px;

  .el-message-box__title {
    justify-content: flex-start;

    span {
      font-size: 16px !important;
      font-weight: bold;

    }
  }

  .el-message-box__message {
    text-align: left !important;

    strong {
      font-size: 18px;
    }

    span {
      font-size: 15px;

    }

  }

  .el-message-box__btns {
    text-align: right !important;
  }



}
</style>
<template>
  <div>
    <el-dialog title="存储策略设置" :append-to-body='true' :visible.sync="SetSaveStrategyShow"
      :before-close="SetSaveStrategyClose" width="1133px" top="3%" center>

      <div slot="title" class="dialog-title">
        <span class="f16 fff" style="color:white">
          存储策略设置
      </span>
      <el-button v-popover:popover type="text">
        <i class="iconfont" style="color:white">&#xe720;</i>
        <!-- <i class="el-icon-question f24" style="color:white"
                          ></i> -->
      </el-button>
      <el-popover ref="popover" placement="right" width="300" trigger="hover">
        <div class="mr10 ml10 mt10 mb10">

            <span> 为确保正常调阅影像及数据安全，请注意：</span><br />

            <span>1、配置近线策略时，指定天数极限值为15天，磁盘容量极限值为60%。除非特殊要求，否则请确保参数大于极限值。</span><br />

            <span>2、配置离线策略时：</span><br />

            <span>（1）指定天数极限值为90天，磁盘容量极限值为80%且最少保留90天。除非特殊要求，否则请确保参数大于极限值；</span><br />

            <span>（2）离线影像时需与校对节点确认影像是否已备份，故配置校对节点后需测试连通性，测试成功方可保存。</span><br />

            <span>3、近线和离线策略同时开启，且都配置为指定天数或指定磁盘容量时，近线参数必须小于离线参数。</span><br />
          </div>

        </el-popover>
      </div>


      <div class="of">
        <el-form :disabled="group !== 'tenancy'" class="of" label-position="right" :model="saveStrategyData"
          :rules="saveStrategyRules" ref="saveStrategyDataForm" style="height: 67vh; overflow-y: auto;">
          <div class="of fl bbd pb10 w100">
            <div class="f18 mb20 clr_333 row">
              <span class="mr20">近线策略</span>
              <el-switch v-model="saveStrategyData.nearline_enable" :active-value="1" :inactive-value="0">
              </el-switch>
              <el-button v-popover:firstPop type="text" v-if="onlineObjectStorage" class="warningDiv f16 ml10">
                    <i class="iconfont">&#xe680;</i>
                  </el-button>
                  <el-popover v-if="onlineObjectStorage" ref="firstPop" placement="right" width="300" trigger="hover">
                    <div class="mr10 ml10 mt10 mb10">
                      <span> 在线设备中有已启用的对象存储设备，只可按指定天数近线。</span>
                    </div>
                  </el-popover>
              <!-- 20230306评审(赵元昊) 存在对象存储设备时，离线策略处直接展示提示文案 -->
              <!-- <span v-if="onlineObjectStorage" class="warningDiv f18 ml10"> <i class="iconfont">&#xe680;</i>
                在线盘中有已启用的对象存储设备，只可按指定天数近线。</span> -->
            </div>
            <div class="w100 mt10 clear_fixed">
              <el-form-item class="inline" prop="nearline_mode">
                <el-radio class="dib ml70" v-model="saveStrategyData.nearline_mode" :label="0"><span></span></el-radio>
              </el-form-item>
              <el-form-item label="近线指定天数前的影像文件：" class="w316 dib mr108">
                <el-input-number style="width: 100px" size="medium" v-model="saveStrategyData.nearyline_of_days"
                  controls-position="right" :min="1"
                  @change="(currentValue, oldValue, label) => daysIptNumChange(currentValue, oldValue, 'nearyline_of_days')"></el-input-number>
                <span class="ml10">天</span>
              </el-form-item>
              <el-form-item class="inline" prop="nearline_mode">
                <!-- 20230306评审(赵元昊) 在线存在对象存储设备时，此处置灰，不可选择 -->
                <el-radio :disabled="onlineObjectStorage" v-model="saveStrategyData.nearline_mode"
                  :label="1"><span></span></el-radio>
              </el-form-item>
              <el-form-item label="近线影像文件到指定磁盘容量：" class="w330 dib">
                <el-input-number style="width: 100px" size="medium" v-model="saveStrategyData.nearline_of_space_ratio"
                  controls-position="right" :min="1"
                  @change="(currentValue, oldValue, label) => ratioIptNumChange(currentValue, oldValue, 'nearline_of_space_ratio')"></el-input-number>
                <span class="ml10">%</span>
              </el-form-item>
            </div>
            <div class="w100 mt10 clear_fixed">
              <el-form-item label="近线策略执行时间：" class="ml70">
                <!-- <el-time-picker
                                      is-range
                                      v-model="nearlinePerformTime"
                                      range-separator="-"
                                      start-placeholder="开始时间"
                                      end-placeholder="结束时间"
                                      placeholder="选择时间范围"
                                      value-format="yyyy-MM-dd HH:mm:ss"
                                      style="width: 240px">
                                    </el-time-picker> -->
                <el-time-picker v-model="nearlinePerformTimeStart" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始时间"
                  style="width: 140px">
                </el-time-picker>
                <span> - </span>
                <el-time-picker v-model="nearlinePerformTimeEnd" value-format="yyyy-MM-dd HH:mm:ss" placeholder="结束时间"
                  style="width: 140px">
                </el-time-picker>
              </el-form-item>
            </div>
          </div>
          <div class="of fl bbd pb10 mt10 w100">
            <div class="f18 mb20 clr_333 row">
              <span class="mr20">离线策略</span>
              <el-switch v-model="saveStrategyData.offline_enable" :active-value="1" :inactive-value="0">
              </el-switch>
              <!-- 20230222评审(赵元昊) 存在对象存储设备时，离线策略处直接展示提示文案 -->
              <!-- <span v-if="hasObjectStorage" class="warningDiv f18 ml10"> <i class="iconfont">&#xe680;</i> 在线盘/近线盘中有已启用的对象存储设备，只可按指定天数离线。</span>-->
                <el-button v-popover:secondPop type="text" v-if="hasObjectStorage" class="warningDiv f16 ml10">
                    <i class="iconfont">&#xe680;</i>
                  </el-button>
                  <el-popover v-if="hasObjectStorage" ref="secondPop" placement="right" width="300" trigger="hover">
                    <div class="mr10 ml10 mt10 mb10">
                      <span> 存储设备中有已启用的对象存储设备，只可按指定天数离线。</span>
                    </div>
                  </el-popover>
                <!--  -->

            </div>
            <div class="w100 mt10 clear_fixed">
              <el-form-item class="inline" prop="offline_mode">
                <el-radio class="dib ml70" v-model="saveStrategyData.offline_mode" :label="0"><span></span></el-radio>
              </el-form-item>
              <el-form-item label="离线指定天数前的影像文件：" class="w316 dib mr108">
                <el-input-number style="width: 100px" size="medium" v-model="saveStrategyData.offline_of_days"
                  controls-position="right" :min="1"
                  @change="(currentValue, oldValue, label) => daysIptNumChange(currentValue, oldValue, 'offline_of_days')"></el-input-number>
                <span class="ml10">天</span>
              </el-form-item>
              <el-form-item class="inline" prop="offline_mode">
                <!-- 20230222评审(赵元昊) 存在对象存储设备时，此处置灰，不可选择 -->
                <el-radio :disabled="hasObjectStorage" v-model="saveStrategyData.offline_mode"
                  :label="1"><span></span></el-radio>
            </el-form-item>
              <el-form-item label="离线影像文件到指定磁盘容量：" class="w330 dib">
                <el-input-number style="width: 100px" size="medium" v-model="saveStrategyData.offline_of_space_ratio"
                  controls-position="right" :min="1"
                @change="(currentValue, oldValue, label) => ratioIptNumChange(currentValue, oldValue, 'offline_of_space_ratio')"></el-input-number>
                <span class="ml10">%</span>
              </el-form-item>
              <el-form-item label="且最少保留" class="dib" style="width: 194px;">
                <el-input-number style="width: 100px" size="medium" v-model="saveStrategyData.offline_save_days"
                controls-position="right" :min="1"
                  @change="(currentValue, oldValue, label) => ratioIptNumChange(currentValue, oldValue, 'offline_save_days')"></el-input-number>
                <span class="ml10">天</span>
              </el-form-item>
            </div>
            <div class="w100 mt10 clear_fixed">
              <el-form-item label="离线策略执行时间：" class="ml70">
                <el-time-picker v-model="offlinePerformTimeStart" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始时间"
                  style="width: 140px">
                </el-time-picker>
                <span> - </span>
                <el-time-picker v-model="offlinePerformTimeEnd" value-format="yyyy-MM-dd HH:mm:ss" placeholder="结束时间"
                  style="width: 140px">
                </el-time-picker>
              </el-form-item>
            </div>
            <div class="w100 mt10">
              <p class="mb10 strong">校对节点</p>
              <el-row class="ml70">
                <el-col :span="8">
                  <el-form-item label="应用标识：" label-width="70px">
                    <el-input style="width: 200px" size="small"
                      v-model="saveStrategyData.storage_commit_node_aet"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="主机：" label-width="70px" prop="storage_commit_node_host">
                    <el-input style="width: 200px" size="small"
                      v-model="saveStrategyData.storage_commit_node_host"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="端口号：" label-width="70px" prop="storage_commit_node_port">
                  <el-input style="width: 200px" size="small"
                    v-model="saveStrategyData.storage_commit_node_port"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                  <el-form-item label="描述：" label-width="70px" class="mt10">
                    <!-- style="width: calc(100% - 70px)" -->
                    <el-input size="small" v-model="saveStrategyData.storage_commit_node_description"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item class="mt10  ml10">
                    <!-- style="width: calc(100% - 70px)" -->
                    <el-button type="primary" size="small" @click="handleTest">测试</el-button>
                    <el-tag class="ml5" v-show="testStatus.show" :type="testStatus.type" size="mini">{{ testStatus.label
                    }}</el-tag>
                    <el-checkbox class="ml5" v-model="saveStrategyData.verify_instance_availability" size="small"
                      :true-label="1" :false-label="0">是否校对检查的在线状态</el-checkbox>


                    <!-- <el-input  size="small"
                                    v-model="saveStrategyData.storage_commit_node_description"></el-input> -->
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                                <el-form-item label-width="70px" class="mt10"> -->
                <!-- v-model="saveStrategyData.nearline_mode" :label="0" -->
                <!-- <el-radio><span>是否校对检查的在线状态</span></el-radio> -->

                <!-- </el-form-item>
                            </el-col> -->
              </el-row>
            </div>
          </div>
          <div class="of fl pb10 mt10 w100">
            <div class="f18 mb20 clr_333">
              <span class="mr20">压缩策略</span>
              <el-switch v-model="saveStrategyData.compress_enable" :active-value="1" :inactive-value="0">
              </el-switch>
            </div>
            <div class="w100 mt10 clear_fixed">
              <el-col :span="12">
                <el-form-item label="压缩指定天数前的影像文件：" class="ml70">
                  <el-input-number style="width: 100px" size="medium" v-model="saveStrategyData.compress_of_days"
                    controls-position="right" :min="1"
                    @change="(currentValue, oldValue, label) => daysIptNumChange(currentValue, oldValue, 'compress_of_days')"></el-input-number>
                  <span class="ml10">天</span>
                </el-form-item></el-col>
              <!-- 20230222 根据赵元昊要求 添加压缩指定范围内的影像文件 -->
              <!-- class="w316 dib mr108 ml70" -->
              <el-col :span="12">
                <el-form-item label="压缩指定范围内的影像文件：">
                  <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="compressPerformTimeRange"
                    type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-form-item label="压缩策略执行时间：" class="dib ml70 mt10" label-width="126px">
                <!-- <el-time-picker
                                      is-range
                                      v-model="compressPerformTime"
                                      range-separator="-"
                                      start-placeholder="开始时间"
                                      end-placeholder="结束时间"
                                      placeholder="选择时间范围"
                                      value-format="yyyy-MM-dd HH:mm:ss"
                                      style="width: 240px">
                                    </el-time-picker> -->
                <el-time-picker v-model="compressPerformTimeStart" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始时间"
                  style="width: 120px">
                </el-time-picker>
                <span> - </span>
                <el-time-picker v-model="compressPerformTimeEnd" value-format="yyyy-MM-dd HH:mm:ss" placeholder="结束时间"
                  style="width: 120px">
                </el-time-picker>
              </el-form-item>
            </div>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer mt20 fr">
          <el-button :disabled="group !== 'tenancy'" @click="SetSaveStrategyClose" size="small" class="mr20">关
            闭</el-button>
          <el-button :disabled="group !== 'tenancy'" type="primary" @click="preUpdateSaveStrategy" size="small">保
            存</el-button>
        </div>
      </div>
    </el-dialog>

    <div class="white-head">
      <el-dialog title="提示" :visible.sync="innerVisible" width="40%" :show-close="false" :before-close="handleClose"
        @opened="openedAlertContent">
        <div slot="title" class="dialog-title fff">
          <i class="iconfont clr_warn" style="font-size: 26px;">&#xe680;</i>
          <span class="f16 ml5">
            以下参数小于极限值，可能会影响影像调阅或数据安全，请确认！
          </span>
        </div>
        <div id="alert_content" class="f16 ml15">

        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateSaveStrategy">
            确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { validPort, validPath, validURL, validIP } from '@/utils/validate'
import moment from 'moment'
export default {
  data() {
    let modeValidator = (type, tip) => {
      return (rule, value, callback) => {
        if (this.saveStrategyData[type] === 1) {
          if (value === '') {
            callback(new Error(tip))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
    }
    let urlValidator = (rule, value, callback) => {
      if (this.saveStrategyData.offline_enable && value !== '') {
        if (validURL(value) || validIP(value)) {
          callback()
        } else {
          callback(new Error('访问地址格式错误'))
        }
      } else {
        callback()
      }
    }
    let portValidator = (rule, value, callback) => {
      if (this.saveStrategyData.offline_enable && value !== '') {
        if (validPort(value)) {
          callback()
        } else {
          callback(new Error('端口号范围为0-65535'))
        }
      } else {
        callback()
      }
    }
    return {
      //20230222评审
      /**
       * 默认值：
       * 近线：360天  容量80%
       * 离线：730天  容量90%且最少保留90天
       */
      saveStrategyData: {
        id: 0,
        nearline_enable: 0,
        nearline_mode: '',
        nearyline_of_days: 360,
        nearline_of_space_ratio: 80,
        nearline_begin_perform_time: '',
        nearline_end_perform_time: '',
        offline_enable: 0,
        offline_mode: '',
        offline_of_days: 730,
        offline_of_space_ratio: 90,
        offline_save_days: 90,
        offline_begin_perform_time: '',
        offline_end_perform_time: '',
        compress_enable: 0,
        compress_of_days: '',
        compress_begin_perform_time: '',
        compress_end_perform_time: '',
        compress_begin_study_date_time: '2020-01-01',
        compress_end_study_date_time: '2030-12-31',
        storage_commit_node_aet: '',
        storage_commit_node_host: '',
        storage_commit_node_port: '',
        storage_commit_node_description: '',
        verify_instance_availability: '',
      },
      saveStrategyRules: {
        nearline_mode: [{
          trigger: 'blur',
          validator: modeValidator('nearline_enable', '请选择近线模式')
        }],
        offline_mode: [{
          trigger: 'blur',
          validator: modeValidator('offline_enable', '请选择离线模式')
        }],
        storage_commit_node_host: [{
          trigger: 'blur',
          validator: urlValidator
        }],
        storage_commit_node_port: [{
          trigger: 'blur',
          validator: portValidator
        }],
      },
      // nearlinePerformTime: null, // 不填写默认为全天执行
      offlinePerformTime: null, // 不填写默认为全天执行
      compressPerformTime: null, // 不填写默认为全天执行
      nearlinePerformTimeStart: null, // 近线策略开始时间
      nearlinePerformTimeEnd: null, // 近线策略执行结束时间
      offlinePerformTimeStart: null, // 离线策略执行开始时间
      offlinePerformTimeEnd: null, // 离线策略执行结束时间
      compressPerformTimeStart: null, // 压缩策略执行开始时间
      compressPerformTimeEnd: null, // 压缩策略执行结束时间
      compressPerformTimeRange: ['2020-01-01', '2030-12-31'],//压缩指定范围内影像选择时间范围
      storageCommitNodeHost: '',//节点ip
      storageCommitNodePort: '',//节点端口
      storageCommitNodeAet: '',//节点标识
      testStatus: {
        show: false,
        label: '默认',
        type: 'warning',
      },
      innerVisible: false,
      innerAlertHTML: '',
      // hasObjectStorage: false,//测试用，等下记得删除
    }
  },
  computed: {
    ...mapGetters(['group'])
  },
  watch: {
    // nearlinePerformTime (newVal) {
    //   console.log(newVal);
    //   if (newVal) {
    //     this.saveStrategyData.nearline_begin_perform_time = newVal[0]
    //     this.saveStrategyData.nearline_end_perform_time = newVal[1]
    //   } else {
    //     this.saveStrategyData.nearline_begin_perform_time = ''
    //     this.saveStrategyData.nearline_end_perform_time = ''
    //   }
    // },
    nearlinePerformTimeStart(newVal) {
      if (newVal) {
        this.saveStrategyData.nearline_begin_perform_time = newVal;
      } else {
        this.saveStrategyData.nearline_begin_perform_time = ''
      }
    },
    nearlinePerformTimeEnd(newVal) {
      if (newVal) {
        this.saveStrategyData.nearline_end_perform_time = newVal;
      } else {
        this.saveStrategyData.nearline_end_perform_time = ''
      }
    },
    // offlinePerformTime (newVal) {
    //   if (newVal) {
    //     this.saveStrategyData.offline_begin_perform_time = newVal[0]
    //     this.saveStrategyData.offline_end_perform_time = newVal[1]
    //   } else {
    //     this.saveStrategyData.offline_begin_perform_time = ''
    //     this.saveStrategyData.offline_end_perform_time = ''
    //   }
    // },
    offlinePerformTimeStart(newVal) {
      if (newVal) {
        this.saveStrategyData.offline_begin_perform_time = newVal;
      } else {
        this.saveStrategyData.offline_begin_perform_time = ''
      }
    },
    offlinePerformTimeEnd(newVal) {
      if (newVal) {
        this.saveStrategyData.offline_end_perform_time = newVal;
      } else {
        this.saveStrategyData.offline_end_perform_time = ''
      }
    },
    //
    // compressPerformTime (newVal) {
    //   if (newVal) {
    //     this.saveStrategyData.compress_begin_perform_time = newVal[0]
    //     this.saveStrategyData.compress_end_perform_time = newVal[1]
    //   } else {
    //     this.saveStrategyData.compress_begin_perform_time = ''
    //     this.saveStrategyData.compress_end_perform_time = ''
    //   }
    // },
    compressPerformTimeStart(newVal) {
      if (newVal) {
        this.saveStrategyData.compress_begin_perform_time = newVal;
      } else {
        this.saveStrategyData.compress_begin_perform_time = ''
      }
    },
    compressPerformTimeEnd(newVal) {
      if (newVal) {
        this.saveStrategyData.compress_end_perform_time = newVal;
      } else {
        this.saveStrategyData.compress_end_perform_time = ''
      }
    },
    SetSaveStrategyShow(newVal) {
      if (newVal) {
        this.getSaveStrategy()
      }
    },
    compressPerformTimeRange(newVal) {
      if (newVal) {
        this.saveStrategyData.compress_begin_study_date_time = newVal[0]
        this.saveStrategyData.compress_end_study_date_time = newVal[1]
      } 
    },
    //节点发生变化，重置测试状态
    'saveStrategyData.storage_commit_node_host'(newVal) {
      if (newVal && newVal != this.storageCommitNodeHost) {        
        this.changeTestStatus(0)
      }
    },
    'saveStrategyData.storage_commit_node_port'(newVal) {
      if (newVal && newVal != this.storageCommitNodePort) {       
        this.changeTestStatus(0)
      }
    },
    'saveStrategyData.storage_commit_node_aet'(newVal) {
      if (newVal && newVal != this.storageCommitNodeAet) {
        this.changeTestStatus(0)
      }
    },
  },
  props: ['SetSaveStrategyShow', 'hasObjectStorage', 'onlineObjectStorage'],//
  methods: {
    handleClose(){
      this.innerVisible = false
    },
    changeTestStatus(status) {

      switch (status) {
        case 0: {
          this.testStatus = {
            show: true,
            label: '请测试',
            type: 'warning',
          }
        } break
        case 1: {
          this.testStatus = {
            show: true,
            label: '成功',
            type: 'success',
          }
        } break
        case 2: {
          this.testStatus = {
            show: true,
            label: '失败,请重试',
            type: 'danger',
          }
        } break
        case 3: {
          this.testStatus = {
            show: false,
            label: '请测试',
            type: 'warning',
          }
        } break
        default: {
          this.testStatus = {
            show: false,
            label: '默认',
            type: 'warning',
          }
        } break
      }

    },
    async handleTest() {


     if(this.testStatus.label === '默认'){
      this.changeTestStatus(3)
     }

      if (this.saveStrategyData.storage_commit_node_aet === '') {
        this.$message.error('请输入校对节点应用标识')
        return
      }
      if (this.saveStrategyData.storage_commit_node_host === '') {
        this.$message.error('请输入校对节点主机')
        return
      }
      if (this.saveStrategyData.storage_commit_node_port === '') {
        this.$message.error('请输入校对节点端口号')
        return
      }
      if (this.saveStrategyData.storage_commit_node_port < 0 || this.saveStrategyData.storage_commit_node_port > 65535) {
        this.$message.error('端口号范围为0~65535')
        return
      }
      let data = {
        storage_commit_node_host: this.saveStrategyData.storage_commit_node_host,
        storage_commit_node_port: this.saveStrategyData.storage_commit_node_port,
        storage_commit_node_aet: this.saveStrategyData.storage_commit_node_aet,
      }
      const res = await this.$pacsApi.pacsApi.getStrategyVerify(data)
      if (res.code === 0) {
        this.storageCommitNodePort = data.storage_commit_node_port
        this.storageCommitNodeAet = data.storage_commit_node_aet
        this.storageCommitNodeHost = data.storage_commit_node_host
        this.$message.success('测试成功')
        this.changeTestStatus(1)
      } else {
        this.$message.error(res.msg)
        this.changeTestStatus(2)
      }
    },

    SetSaveStrategyClose() {
      this.$emit('SetSaveStrategyClose', '')
    },
    async getSaveStrategy() {
      const res = await this.$pacsApi.pacsApi.getSaveStrategy()
//       const res = {
//     "data": {
//         "id": "1432947409301803008",
//         "nearline_enable": 1,
//         "nearline_mode": 1,
//         "nearline_begin_perform_time": "2023-01-01 18:00:00",
//         "nearline_end_perform_time": "2023-01-01 19:05:00",
//         "nearyline_of_days": 15,
//         "nearline_of_space_ratio": 80,
//         "offline_enable": 1,
//         "offline_mode": 1,
//         "offline_begin_perform_time": "2000-01-01 23:30:00",
//         "offline_end_perform_time": "2000-01-01 23:59:59",
//         "offline_of_days": 90,
//         "offline_of_space_ratio": 85,
//         "offline_save_days": 90,
//         "compress_enable": 1,
//         "compress_begin_perform_time": "2000-01-01 08:00:00",
//         "compress_end_perform_time": "2023-01-01 18:59:53",
//         "compress_of_days": 2600,
//         "storage_commit_node_host": "123.123.123.123",
//         "storage_commit_node_port": 123,
//         "storage_commit_node_aet": "aet",
//         "storage_commit_node_description": "abc"
//     },
//     "code": 0,
//     "msg": "ok"
// }
      const { code, data } = res
      if (code === 0) {
        this.storageCommitNodePort = data.storage_commit_node_port
        this.storageCommitNodeAet = data.storage_commit_node_aet
        this.storageCommitNodeHost = data.storage_commit_node_host

        this.saveStrategyData = data
        // this.nearlinePerformTime = [data.nearline_begin_perform_time, data.nearline_end_perform_time]
        this.nearlinePerformTimeStart = data.nearline_begin_perform_time;
        this.nearlinePerformTimeEnd = data.nearline_end_perform_time;
        // this.offlinePerformTime = [data.offline_begin_perform_time, data.offline_end_perform_time]
        this.offlinePerformTimeStart = data.offline_begin_perform_time;
        this.offlinePerformTimeEnd = data.offline_end_perform_time;
        // this.compressPerformTime = [data.compress_begin_perform_time, data.compress_end_perform_time]
        this.compressPerformTimeStart = data.compress_begin_perform_time;
        this.compressPerformTimeEnd = data.compress_end_perform_time;
        if (!!data.compress_begin_study_date_time )
        {
          this.saveStrategyData.compress_begin_study_date_time = data.compress_begin_study_date_time;
        }else{
          this.saveStrategyData.compress_begin_study_date_time = '2020-01-01'
        }
        
        if (!!data.compress_end_study_date_time) {
          this.saveStrategyData.compress_end_study_date_time =  data.compress_end_study_date_time;
        }else{
          this.saveStrategyData.compress_end_study_date_time =  '2030-12-31';
        }
        this.compressPerformTimeRange = [this.saveStrategyData.compress_begin_study_date_time,this.saveStrategyData.compress_end_study_date_time]
      }
    },
    openedAlertContent() {

      let divDocument = document.getElementById('alert_content')
      divDocument.innerHTML = this.innerAlertHTML
    },
    formattingTime(str) {
      let date = new Date(str)
      return '2023-01-01 ' + moment(date).format("HH:mm:ss")
    },
    validTimes(begin, end) {
      let startStr = this.formattingTime(begin)
      let endStr = this.formattingTime(end)
      let timeL = new Date(endStr).getTime() - new Date(startStr).getTime()

      if (timeL >= 0 && timeL < 5 * 60 * 1000) return true
      return false
    },
    ValidationStrategyRules() {

      if(this.onlineObjectStorage && this.saveStrategyData.nearline_enable === 1 && this.saveStrategyData.nearline_mode==1)
      {
        this.$message.error('在线盘中有已启用的对象存储设备，近线策略只可按指定天数近线！')
        return
      }
      if(this.hasObjectStorage && this.saveStrategyData.offline_enable === 1 && this.saveStrategyData.offline_mode==1)
      {
        this.$message.error('在线盘/近线盘中有已启用的对象存储设备，离线策略只可按指定天数离线！')
        return
      }
      //存储策略极限值及冲突规则提示
      //判断校对节点是否异常--离线策略开启才需要校对节点
      // console.log('测试情况哦',this.testStatus.label,this.saveStrategyData,this.saveStrategyData.offline_enable);
      if (this.testStatus.label !== '成功' && this.testStatus.label !== '默认' && this.saveStrategyData.offline_enable === 1) {
        this.$message.error('校对节点异常，请测试！')
        return
      }
      if (this.saveStrategyData.nearline_enable && this.saveStrategyData.offline_enable && this.saveStrategyData.nearline_mode === 0 && this.saveStrategyData.offline_mode === 0 && this.saveStrategyData.nearyline_of_days >= this.saveStrategyData.offline_of_days) {
        this.$message.error(`近线策略指定天数[${this.saveStrategyData.nearyline_of_days}]天，需小于离线策略指定天数[${this.saveStrategyData.offline_of_days}]天！`)
        return
      }
      if (this.saveStrategyData.nearline_enable && this.saveStrategyData.offline_enable && this.saveStrategyData.nearline_mode === 1 && this.saveStrategyData.offline_mode === 1 && this.saveStrategyData.nearline_of_space_ratio >= this.saveStrategyData.offline_of_space_ratio) {
        this.$message.error(`近线策略指定磁盘容量[${this.saveStrategyData.nearline_of_space_ratio}%]，需小于离线策略指定磁盘容量[${this.saveStrategyData.offline_of_space_ratio}%]！`)
        return
      }
      const timer = this.validTimes(this.nearlinePerformTimeStart, this.nearlinePerformTimeEnd)

      // const timer = new Date(this.nearlinePerformTimeEnd).getTime() - new Date(this.nearlinePerformTimeStart).getTime()
      if (this.saveStrategyData.nearline_enable && timer ) {
        this.$message.warning('近线策略执行时间间隔应大于5分钟')
        return
      }
      const timer2 = this.validTimes(this.offlinePerformTimeStart, this.offlinePerformTimeEnd)

      // const timer2 = new Date(this.offlinePerformTimeEnd).getTime() - new Date(this.offlinePerformTimeStart).getTime()
      if (this.saveStrategyData.offline_enable && timer2 ) {
        this.$message.warning('离线策略执行时间间隔应大于5分钟')
        return
      }
      let alertContent = '<strong>参数小于极限值:</strong><br/>'
      let count = 1
      //测试用   
      if (this.saveStrategyData.nearline_enable && this.saveStrategyData.nearline_mode === 0 && this.saveStrategyData.nearyline_of_days < 15) {
        // this.$message.warning('“近线指定天数前的影像文件”应大于15天')
        alertContent = alertContent + `<span>${count}、近线指定天数为${this.saveStrategyData.nearyline_of_days}天，小于极限值15天</span><br/>`
        count++
        // return
      }
      if (this.saveStrategyData.nearline_enable && this.saveStrategyData.nearline_mode === 1 && this.saveStrategyData.nearline_of_space_ratio < 60) {
        //20230222评审    近线极限值 15天 容量60%
        // this.$message.warning('“近线影像文件到指定磁盘容量”应大于60%')
        // return
        alertContent = alertContent + `<span>${count}、近线指定磁盘容量为${this.saveStrategyData.nearline_of_space_ratio}%，小于极限值60%</span><br/>`
        count++
      }

      if (this.saveStrategyData.offline_enable && this.saveStrategyData.offline_mode === 0 && this.saveStrategyData.offline_of_days < 90) {
        // this.$message.warning('“离线指定天数前的影像文件”应大于90天')
        // return
        alertContent = alertContent + `<span>${count}、离线指定天数为${this.saveStrategyData.offline_of_days}天，小于极限值90天</span><br/>`
        count++
      }
      if (this.saveStrategyData.offline_enable && this.saveStrategyData.offline_mode === 1 && this.saveStrategyData.offline_of_space_ratio < 80) {
        // this.$message.warning('“离线影像文件到指定磁盘容量”应大于80%且最少保留90天')
        // return
        alertContent = alertContent + `<span>${count}、离线指定磁盘容量为${this.saveStrategyData.offline_of_space_ratio}%，小于极限值80%</span><br/>`
        count++
      }

      if (this.saveStrategyData.offline_enable && this.saveStrategyData.offline_mode === 1 && this.saveStrategyData.offline_save_days < 90) {
        // this.$message.warning('“离线影像文件到指定磁盘容量”应大于80%且最少保留90天')
        // return
        alertContent = alertContent + `<span>${count}、离线最少保留天数为${this.saveStrategyData.offline_save_days}天，小于极限值90天</span><br/>`
        count++
      }

      let configCount = 0;
      let configStr = '<strong>策略配置不一致</strong><br/>'
      if (this.saveStrategyData.offline_enable && this.saveStrategyData.nearline_enable && this.saveStrategyData.offline_mode !== this.saveStrategyData.nearline_mode) {
        if (this.saveStrategyData.offline_mode === 0) {
          //离线天数、近线容量
          configStr = configStr + `近线策略为指定容量，离线策略为指定天数，两者不一致，请确认配置无误！`
        } else {
          configStr = configStr + `近线策略为指定天数，离线策略为指定容量，两者不一致，请确认配置无误！`

        }
        if (count > 1) {
          alertContent = configStr + '<br/><br/>' + alertContent;
        } else {
          alertContent = configStr;
        }
        configCount++;
      }



      if (count > 1 || configCount > 0) {
        // this.innerAlertHTML = alertContent
        // this.innerVisible = true
        this.$confirm(alertContent, '以下设置可能会影响影像调阅或数据安全,请确认!', {
          confirmButtonText: '取消',
          cancelButtonText: '确认',
          type: 'warning',
          center: true,
          dangerouslyUseHTMLString: true,
          distinguishCancelAndClose	: true,
        }).then((action) => {

          //取消操作不管
          // this.$message({
          //   type: 'success',
          //   message: '离线成功!'
          // });
        }).catch((action) => {
          if(action === 'cancel'){
            //这里改为确认操作了
            this.updateSaveStrategy();
          }else{
          //取消操作不管
          }
          // this.$message({
          //   type: 'info',
          //   message: '已取消离线'
          // });
        });


      } else {
        this.updateSaveStrategy()
      }

    },
    preUpdateSaveStrategy() {
      this.$refs.saveStrategyDataForm.validate(async (valid) => {
        if (valid) {
          this.ValidationStrategyRules()
        }
      })
    },

    async updateSaveStrategy() {

      const res = await this.$pacsApi.pacsApi.updateSaveStrategy(this.saveStrategyData)
      const { code, msg } = res
      if (code === 0) {
        this.innerVisible = false
        this.SetSaveStrategyClose()
        this.$message({
          type: 'success',
          message: msg
        })
        //更新存储策略成功！
      } else {
        this.innerVisible = false
        //更新存储策略失败，请重试！
        this.$message.error(msg)
      }

    },
    daysIptNumChange(currentValue, oldValue, label) {
      if (currentValue === undefined) {
        this.$nextTick(() => {
          this.saveStrategyData[label] = 1
        })
      } else if (!Number.isInteger(currentValue)) {
        this.$nextTick(() => {
          this.saveStrategyData[label] = parseInt(currentValue)
        })
      }
    },
    ratioIptNumChange(currentValue, oldValue, label) {
      if (currentValue === undefined) {
        this.$nextTick(() => {
          if (label === 'offline_save_days') {
            this.saveStrategyData[label] = 90
          } else {
            this.saveStrategyData[label] = 0
          }
        })
      }
    }
  }
}
</script>
